package com.wy.demo.jsonFormat.fastJson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.*;

public class JsonDemo {
    private User1 testUser1;
    private File jsonFile;

    @Before
    public void setUp() {
        testUser1 = new User1(1L, "张三", "zhangsan@example.com", new Date(), 
            Arrays.asList("读书", "游泳", "编程"));
        jsonFile = new File("src/main/resources/User1.json");
    }

    @Test
    public void testObjectToJsonString() {
        // 对象转 JSON 字符串
        String jsonString = JSON.toJSONString(testUser1, SerializerFeature.PrettyFormat);
        System.out.println("对象转 JSON 字符串：");
        System.out.println(jsonString);
        
        assertNotNull(jsonString);
        assertTrue(jsonString.contains("张三"));
        assertTrue(jsonString.contains("email_address"));
    }

    @Test
    public void testJsonStringToObject() {
        // 对象转 JSON 字符串
        String jsonString = JSON.toJSONString(testUser1);
        
        // JSON 字符串转对象
        User1 parsedUser1 = JSON.parseObject(jsonString, User1.class);
        System.out.println("\nJSON 字符串转对象：");
        System.out.println("姓名：" + parsedUser1.getName());
        System.out.println("邮箱：" + parsedUser1.getEmail());
        
        assertEquals(testUser1.getName(), parsedUser1.getName());
        assertEquals(testUser1.getEmail(), parsedUser1.getEmail());
    }

    @Test
    public void testObjectToJsonFile() throws IOException {
        // 对象转 JSON 文件
        String jsonString = JSON.toJSONString(testUser1, SerializerFeature.PrettyFormat);
        Files.write(Paths.get(jsonFile.getPath()), jsonString.getBytes());
        System.out.println("\n对象已写入文件：" + jsonFile.getAbsolutePath());
        
        assertTrue(jsonFile.exists());
        assertTrue(jsonFile.length() > 0);
    }

    @Test
    public void testJsonFileToObject() throws IOException {
        // 从文件读取 JSON 到对象
        String jsonString = new String(Files.readAllBytes(Paths.get(jsonFile.getPath())));
        User1 fileUser1 = JSON.parseObject(jsonString, User1.class);
        System.out.println("\n从文件读取 JSON 到对象：");
        System.out.println("姓名：" + fileUser1.getName());
        System.out.println("爱好：" + fileUser1.getHobbies());
        
        assertNotNull(fileUser1);
        assertNotNull(fileUser1.getName());
        assertNotNull(fileUser1.getHobbies());
    }

    @Test
    public void testJsonObjectOperations() {
        // 创建 JSONObject
        JSONObject jsonObject = (JSONObject) JSON.toJSON(testUser1);
        
        System.out.println("\nJSONObject 操作：");
        System.out.println("获取 name 字段：" + jsonObject.getString("name"));
        System.out.println("获取 hobbies 数组：" + jsonObject.getJSONArray("hobbies"));
        
        assertTrue(jsonObject.containsKey("name"));
        assertTrue(jsonObject.containsKey("hobbies"));
        assertEquals("张三", jsonObject.getString("name"));
    }

    @Test
    public void testJsonArrayOperations() {
        // 创建用户数组
        User1[] User1s = new User1[]{
            new User1(1L, "张三", "zhangsan@example.com", new Date(), Arrays.asList("读书", "游泳")),
            new User1(2L, "李四", "lisi@example.com", new Date(), Arrays.asList("篮球", "足球"))
        };
        
        // 数组转 JSON
        String jsonArray = JSON.toJSONString(User1s, SerializerFeature.PrettyFormat);
        System.out.println("\n数组转 JSON：");
        System.out.println(jsonArray);
        
        // JSON 转数组
        User1[] parsedUser1s = JSON.parseObject(jsonArray, User1[].class);
        System.out.println("\nJSON 转数组：");
        for (User1 User1 : parsedUser1s) {
            System.out.println("姓名：" + User1.getName());
        }
        
        assertEquals(2, parsedUser1s.length);
        assertEquals("张三", parsedUser1s[0].getName());
        assertEquals("李四", parsedUser1s[1].getName());
    }

    @Test
    public void testJsonPathOperations() {
        // 创建复杂的 JSON 对象
        JSONObject root = new JSONObject();
        root.put("User1", JSON.toJSON(testUser1));
        root.put("timestamp", new Date().getTime());
        
        // 使用 JSONPath 获取值
        String name = JSONPath.eval(root, "$.User1.name").toString();
        List<String> hobbies = (List<String>) JSONPath.eval(root, "$.User1.hobbies");
        
        System.out.println("\nJSONPath 操作：");
        System.out.println("获取用户名：" + name);
        System.out.println("获取爱好：" + hobbies);
        
        assertEquals("张三", name);
        assertNotNull(hobbies);
    }
} 